<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li{
            list-style: none;
            width: 500px;
        }
        .title{
            margin-left: 40px;
            height:24px;
            background-color: rgba(0, 0, 255, 0.363);
            width: 400px;
            text-align: center;
        }
        input{
            width: 60px;
        }
        .footer{
            background-color: orange;
            width: 370px;
            height: 28px;
            margin-left: 40px;
            padding-right: 30px;
        }
        .fr{
            float: right;
        }
        a{
            cursor: pointer;
            color: purple;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>
<body>
    <div id="app">
        <!-- <div>我的商品</div>
        <ul>
            <li>
                <img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/0cadc8b00dbe3b5615bd6ab657715baf.png?thumb=1&w=60&h=60&f=webp&q=90">
                <span>Redmi 9</span>
                <button>-</button>
                <input type="text">
                <button>+</button>
                <img src="">
            </li>
            <li>
                <img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/77bfd346ad97807237beca297cfe2fba.png?thumb=1&w=60&h=60&f=webp&q=90">
                <span>Redmi 8A</span>
                <button>-</button>
                <input type="text">
                <button>+</button>
                <img src="">
            </li>
            <li>
                <img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/afa28e7477639537f556eb46e3ca5f43.jpeg?thumb=1&w=60&h=60&f=webp&q=90">
                <span>Redmi 10X</span>
                <button>-</button>
                <input type="text">
                <button>+</button>
                <img src="">
            </li>
            <li>
                <img src="https://cdn.cnbj0.fds.api.mi-img.com/b2c-mimall-media/85d746207b997873ddfcdca0da025f78.jpg?thumb=1&w=60&h=60">
                <span>Redmi 7A</span>
                <button>-</button>
                <input type="text">
                <button>+</button>
                <img src="">
            </li>
            <li>
                <img src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/b4534b9fd22f478314f3cbbf286b7338.png?thumb=1&w=60&h=60&f=webp&q=90">
                <span>Redmi Note 8</span>
                <button>-</button>
                <input type="text">
                <button>+</button>
                <img src="">
            </li>
        </ul>
        <div>
            <span>总价：</span>
            <button>结算</button>
        </div> -->
        <header-buy></header-buy>
        <middle-buy :object="object" @delete-object="deleteObject($event)"></middle-buy>
        <footer-buy></footer-buy>
    </div>



    
    <script>
        Vue.component('header-buy',{
            template: `
            <div class="title">我的商品</div>
            `
        });
        Vue.component('middle-buy',{
            props: ['object'],
            template:`
            <ul>
                <li :key="item.id" v-for="item in object">
                    <img :src="item.url">
                    <span>{{ item.name }}</span>
                    <button @click="subtract(item)">-</button>
                    <input type="text" v-model="item.number">
                    <button @click="item.number ++">+</button>
                    <a @click="$emit('delete-object',item.id)">删除商品</a>
                </li>
            </ul>
            `,
            methods: {
                subtract: function(item){
                    if(item.number > 0){
                        item.number --;
                    }
                }
            }
        });
        Vue.component('footer-buy',{
            props: ['object'],
            template: `
            <div class="footer">
                <button class="fr" style="margin-top:2px">结算</button>
                <span class="fr" style="line-height:28px">总价：</span>
            </div>
            `
        })
        var vm = new Vue({
            el: '#app',
            data: {
                total: 0,
                object: [{
                    id: 0,
                    name: 'Redmi 9',
                    url: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/0cadc8b00dbe3b5615bd6ab657715baf.png?thumb=1&w=60&h=60&f=webp&q=90',
                    price: 799,
                    number: 1
                },{
                    id: 1,
                    name: 'Redmi 8A',
                    url: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/77bfd346ad97807237beca297cfe2fba.png?thumb=1&w=60&h=60&f=webp&q=90',
                    price: 599,
                    number: 1
                },{
                    id: 2,
                    name: 'Redmi 10X',
                    url: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/afa28e7477639537f556eb46e3ca5f43.jpeg?thumb=1&w=60&h=60&f=webp&q=90',
                    price: 1199,
                    number: 1
                },{
                    id: 3,
                    name: 'Redmi 7A',
                    url: 'https://cdn.cnbj0.fds.api.mi-img.com/b2c-mimall-media/85d746207b997873ddfcdca0da025f78.jpg?thumb=1&w=60&h=60',
                    price: 549,
                    number: 1
                },{
                    id: 4,
                    name: 'Redmi Note 8',
                    url: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/b4534b9fd22f478314f3cbbf286b7338.png?thumb=1&w=60&h=60&f=webp&q=90',
                    price: 949,
                    number: 1
                }]
            },
            methods: {
                deleteObject: function(id){
                    // console.log(typeof(id));
                    // console.log(this.object[id - 1]);
                    this.object.splice(id,1);
                }
            }
        });
    </script>

</body>
</html>